#include <buola/chart/c3daxis.h>

#include <buola/image/cgc.h>

namespace buola { namespace chart {

C3DAxis::C3DAxis(EAxis pAxis)
    :   mAxis(pAxis)
{
    if(mAxis==AXIS_X)
    {
        mVertices[0]={0.0,1.0,-0.7};
        mVertices[1]={0.0,-1.0,-0.7};
        mVertices[2]={0.0,-1.0,0.7};
        mVertices[3]={0.0,1.0,0.7};
        mVector={-1.0,0.0,0.0};
        mOrigin=-1.0;
    }
    else if(mAxis==AXIS_Y)
    {
        mVertices[0]={1.0,0.0,0.7};
        mVertices[1]={1.0,0.0,-0.7};
        mVertices[2]={-1.0,0.0,-0.7};
        mVertices[3]={-1.0,0.0,0.7};
        mVector={0.0,1.0,0.0};
        mOrigin=-1.0;
    }
    else if(mAxis==AXIS_Z)
    {
        mVertices[0]={1.0,1.0,0.0};
        mVertices[1]={1.0,-1.0,0.0};
        mVertices[2]={-1.0,-1.0,0.0};
        mVertices[3]={-1.0,1.0,0.0};
        mVector={0.0,0.0,1.0};
        mOrigin=-0.7;
    }
}

C3DAxis::~C3DAxis()
{
}

void C3DAxis::CalcLimits(double pMin,double pMinPos,double pMax)
{
    mMapping->SetDestLimits(mOrigin,-mOrigin);

    if(mAutoScale)
    {
        mMapping->AutoScale(pMin,pMinPos,pMax,false,0.3);
    }
}

void C3DAxis::Draw(CXYZChartContext &pC)
{
#if 0
    glbColor(CColor(WHITE));
    glBegin(GL_QUADS);
    for(int i=0;i<4;i++)
    {
        C3DVector lP=mVertices[i]+mVector*mOrigin;
        glVertex3d(lP.x,lP.y,lP.z);
    }
    glEnd();
    glbColor(CColor(BLACK));
    glBegin(GL_LINE_LOOP);
    for(int i=0;i<4;i++)
    {
        C3DVector lP=mVertices[i]+mVector*mOrigin;
        glVertex3d(lP.x,lP.y,lP.z);
    }
    glEnd();

/*  if(mLabelNames.size())
    {
        pC.mGC->SetSourceColor(BLACK);

        for(int i=0;i<mLabelNames.size();i++)
        {
            CPoint_d lPoint(Map(0.5+i),mRect.b);
            
            pC.mGC->PushState();

            pC.mGC->Translate(lPoint);
            pC.mGC->Rotate(-M_PI/3);
            pC.mGC->DrawText(CPoint_d(-5,0),mLabelNames[i],NFont::RIGHT|NFont::VCENTER);

            pC.mGC->PopState();
        }
    }
    else
*/  {
        //bool lTopTicks=pC.mChart->GetStyleBit(CXYChart::BOX);

        std::vector<double> lTicks;
        mMapping->GetTicks(lTicks,0.15);

        glLineStipple(1,0x1111u);
        glEnable(GL_LINE_STIPPLE);
        for(int i=0;i<lTicks.size();i++)
        {
            double lXPos=Map(lTicks[i]);

            glBegin(GL_LINE_STRIP);
            for(int i=0;i<3;i++)
            {
                C3DVector lP=mVertices[i]+mVector*lXPos;
                glVertex3d(lP.x,lP.y,lP.z);
            }
            glEnd();
/*          if(lThin)
                lXPos=round(lXPos-0.5)+0.5;
            pC.mGC->SetSourceColor(BLACK);
            pC.mGC->MoveTo(CPoint_d(lXPos,mRect.b-7));
            pC.mGC->LineTo(CPoint_d(lXPos,mRect.b));
            pC.mGC->StrokePath();
        
            if(lTopTicks)
            {
                pC.mGC->MoveTo(CPoint_d(lXPos,mRect.t));
                pC.mGC->LineTo(CPoint_d(lXPos,mRect.t+7));
                pC.mGC->StrokePath();
            }
        
            pC.mGC->DrawText(CPoint(lXPos,mRect.b+4),to_wstring(lTicks[i]),NFont::HCENTER);
        
            pC.mGC->SetLineStyle(CGC::LINE_ONOFF);
            pC.mGC->SetSourceColor(lLinesColor);
            pC.mGC->MoveTo(CPoint_d_d(lXPos,lTopTicks?(mRect.t+7):mRect.t));
            pC.mGC->LineTo(CPoint_d(lXPos,mRect.b-7));
            pC.mGC->StrokePath();
            pC.mGC->SetLineStyle(CGC::LINE_SOLID);
*/      }
        glDisable(GL_LINE_STIPPLE);

/*      if(mTitle.length())
        {
            pC.mGC->SetSourceColor(BLACK);
            pC.mGC->DrawText(CPoint_d((mRect.l+mRect.r)/2.0,mRect.b+20),mTitle,NFont::CENTER);
        }
        if(mMark.mEnabled)
        {
            double lXPos=Map(mMark.mPos);
            if(lThin)
                lXPos=round(lXPos-0.5)+0.5;
            pC.mGC->SetSourceColor(RGBA(0,0,0,150));
            pC.mGC->MoveTo(CPoint_d(lXPos,mRect.t));
            pC.mGC->LineTo(CPoint_d(lXPos,mRect.b));
            pC.mGC->StrokePath();
        }
*/  }
#endif
}

/*namespace chart*/ } /*namespace buola*/ }
